<template>
  <div>
    <div class="login-container" id='login'>
      <div class="title-container" v-if="changeFinish">
        <el-form class="login-form"
                 autoComplete="on"
                 :rules="loginRules"
                 :model="loginForm"
                 ref="loginForm"
                 label-position="left">
          <h1 class="title">恭喜您,新密码设置成功</h1>
        </el-form>
      </div>
      <div v-else>
        <el-form class="login-form"
                 autoComplete="on"
                 :rules="loginRules"
                 :model="loginForm"
                 ref="loginForm"
                 label-position="left">
          <div class="title-container">
            <h3 class="title">设置您的密码</h3>
          </div>
          <el-form-item prop="userName">
            <span class="svg-container svg-container_login">
              <icon name="user"></icon>
            </span>
            <el-input name="userName"
                      type="text"
                      :disabled="true"
                      v-model="loginForm.userName"
                      placeholder="用户名" />
          </el-form-item>
          <el-form-item prop="password">
            <span class="svg-container">
              <icon name="lock"></icon>
            </span>
            <el-input name="password"
                      :type="passwordBlur?'password':'text'"
                      v-model="loginForm.password"
                      placeholder="新密码">
              <i slot="suffix"
                 class="el-icon-view"
                 @click="changePasswordBlur"></i>
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <span class="svg-container">
              <icon name="lock"></icon>
            </span>
            <el-input name="password"
                      :type="passwordBlur?'password':'text'"
                      v-model="loginForm.passwordConfirm"
                      autoComplete="on"
                      placeholder="确认密码">
              <i slot="suffix"
                 class="el-icon-view"
                 @click="changePasswordBlur"></i>
            </el-input>
          </el-form-item>
          <el-button type="primary"
                     class="loginBtn"
                     @click.native.prevent="handleChangePassword">
            确认修改
          </el-button>
        </el-form>
      </div>
    </div>
  </div>

</template>
<script>
import md5 from 'js-md5'
export default {
  name: 'login',
  data () {
    const validatePasswordConfirm = (rule, value, callback) => {
      if (value === '') {
        if (!this.currentSelectUserId) {
          callback(new Error('请再次输入密码'))
        } else {
          callback()
        }
      } else if (value !== this.userForm.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value === '' || !value) {
        if (!this.currentSelectUserId) {
          callback(new Error('请输入密码'))
        } else {
          callback()
        }
      } else {
        if (value.length < 6) {
          callback(new Error('密码长度不小于6位'))
        } else if (value.length > 18) {
          callback(new Error('密码长度不大于18位'))
        } else if (/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z!@#$%^&*()]{6,18}$/.test(value) === false) {
          callback(new Error('密码必须是数字和字母组合'))
        } else {
          callback()
        }
      }
    }
    return {
      apiName: {
        verifyToken: this.apiType.changePwd.verifyToken,
        changePwd: this.apiType.changePwd.change
      },
      loginRules: {
        originalPassword: [
          {required: true, trigger: 'blur', message: '用户名不能为空'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {validator: validatePassword, trigger: 'blur'}
        ],
        passwordConfirm: [
          {required: true, message: '请输入确认密码', trigger: 'blur'},
          {validator: validatePasswordConfirm, trigger: 'blur'}
        ]
      },
      loginForm: {
        userName: this.$route.query.userId,
        password: '',
        passwordConfirm: ''
      },
      passwordBlur: true,
      changeFinish: false
    }
  },
  mounted () {
    this.validateToken()
  },
  methods: {
    changePasswordBlur () {
      this.passwordBlur = !this.passwordBlur
    },
    validateToken () {
      const param = {
        token: this.$route.query.token,
        userId: this.$route.query.userId
      }
      this.fetch(this.apiName.verifyToken, param, null, null, errData => {
        console.log(errData)
        this.$router.push({path: '/404'})
      })
    },
    handleChangePassword () {
      const _this = this
      const body = {
        token: this.$route.query.token,
        userId: this.$route.query.userId,
        password: md5(this.loginForm.password),
        passwordConfirm: md5(this.loginForm.passwordConfirm)
      }
      this.fetch(this.apiName.changePwd, null, body, respData => {
        this.$message({
          showClose: true,
          message: respData.data.data,
          type: 'success',
          duration: 2000
        })
        _this.changeFinish = true
      },
      errData => {
        console.log(errData)
      })
    }

  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
  $bg: #2d3a4b;
  $light_gray: #eee;

  /* reset element-ui css */
  .login-container {
    .login-form {
      .el-form-item__error {
        position: absolute;
      }
    }

    .el-input {
      display: inline-block;
      height: 47px;
      width: 89%;

      input {
        background: transparent;
        border: 0px;
        -webkit-appearance: none;
        border-radius: 0px;
        padding: 12px 5px 12px 15px;
        color: $light_gray;
        height: 47px;

        &:-webkit-autofill {
          -webkit-box-shadow: 0 0 0px 1000px $bg inset !important;
          -webkit-text-fill-color: #fff !important;
        }
      }
      input[disabled] {
        background: transparent;
        border: 0px;
        -webkit-appearance: none;
        border-radius: 0px;
        padding: 12px 5px 12px 15px;
        color: $light_gray;
        height: 47px;

        &:-webkit-autofill {
          -webkit-box-shadow: 0 0 0px 1000px $bg inset !important;
          -webkit-text-fill-color: #fff !important;
        }
      }
    }

    .el-form-item {
      border: 1px solid rgba(255, 255, 255, 0.1);
      background: rgba(0, 0, 0, 0.1);
      border-radius: 5px;
      color: #454545;
      margin-bottom: 20px;
    }

    .el-form-item__content {
      display: block;
      margin-left: 0px !important;
    }

    .el-form-item__error {
      position: absolute;
      padding-top: 5px !important;
    }

    .el-tabs__item {
      color: #889aa4 !important;
    }
  }
</style>
<style rel="stylesheet/scss" lang="scss" scoped>
  $bg: #2d3a4b;
  $dark_gray: #889aa4;
  $light_gray: #eee;

  .login-container {
    position: fixed;
    height: 100%;
    width: 100%;
    background-color: $bg;

    .login-form {
      position: absolute;
      left: 0;
      right: 0;
      width: 400px;
      padding: 35px 35px 15px 35px;
      margin: 120px auto;
    }

    .tips {
      font-size: 14px;
      color: #fff;
      margin-bottom: 10px;

      span {
        &:first-of-type {
          margin-right: 16px;
        }
      }
    }

    .svg-container {
      padding: 6px 5px 6px 15px;
      color: $dark_gray;
      vertical-align: middle;
      width: 30px;
      display: inline-block;

      &_login {
        font-size: 20px;
      }
    }

    .title-container {
      position: relative;

      .title {
        font-size: 26px;
        font-weight: 400;
        color: $light_gray;
        margin: 0px auto 40px auto;
        text-align: center;
        font-weight: bold;
      }

      .set-language {
        color: #fff;
        position: absolute;
        top: 5px;
        right: 0px;
      }
    }

    .show-pwd {
      position: absolute;
      right: 10px;
      top: 7px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
      user-select: none;
    }

    .thirdparty-button {
      position: absolute;
      right: 35px;
      bottom: 28px;
    }

    .el-icon-view {
      vertical-align: middle;
      padding: 16px 5px 6px 15px;
    }

    .loginBtn {
      width: 100%;
      margin-bottom:30px;
      float: left;
    }

    .ssoBtn {
      width: 35%;
      margin-right: 20px;
      float: right;
    }

    .qr-container {
      width: 100%;
      margin: 0 auto;
      text-align: center;
    }
  }
</style>
